/*
 *	Features:
 *		Order Ascending and Descending by clicking the Col Headers
 *		Toolbar Popper
 *		Delete
 *		Delete All
 *		Refresh - in GUI
 */

package text.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import text.util.DBConnector;

public class TemplatesViewer extends Viewer {

	private JTable templatesTable = null;
	private JButton buttonDelTemplates;
	private JButton buttonDelAllTemplates;

	public void setEnabled(boolean bEnabled) {
		if(buttonDelTemplates != null) {
			buttonDelTemplates.setEnabled(bEnabled);
		}

		if(buttonDelAllTemplates != null) {
			buttonDelAllTemplates.setEnabled(bEnabled);
		}
	}
	
	/**
	 * main method allows us to run as a standalone demo.
	 */
	public static void main(String[] args) {
		TemplatesViewer templatesViewer = new TemplatesViewer(null);
		templatesViewer.init();
	}
	
	/**
	 * templatesViewer Constructor
	 */
	public TemplatesViewer(GUI gui) {
		// Set the title for this demo, and an icon used to represent this
		// demo inside the GUI app.
		super(gui, "TemplatesViewer", null);
//		super(gui);
		templatesTable = createTable(createTableModel());
	}
	
	private TableModel createTableModel() {
//		Vector<String> vColumnNames = new Vector<String>();
		Vector vColumnNames = new Vector();
		vColumnNames.add("Delete");
		vColumnNames.add("Templates #");
		vColumnNames.add("English Templates");
		vColumnNames.add("Filipino Templates");
		vColumnNames.add("English Compound Id");
		vColumnNames.add("Filipino Compound Id");
//		Vector<Vector> vData = getTemplatesData();
		Vector vData = getTemplatesData();

		TableModel templatesTableModel = new DefaultTableModel(vData, vColumnNames){
			public Class getColumnClass(int c) {
				Class cClass = null;
				
				//hard coded: second col is an INTEGER for correct sorting of ID's
				if(c == 1) {
					try {
						cClass = Class.forName("java.lang.Integer");
					}catch(ClassNotFoundException e) {
						e.printStackTrace();
					}
				}else {
					cClass = getValueAt(0, c).getClass();
				}
				
				return cClass;
			}
		};
		
		return templatesTableModel;
	}

	private JTable createTable(TableModel tableModel) {
		
		TableSorter sorter = new TableSorter(tableModel);
		JTable table = new JTable(sorter){
/*			public boolean isCellEditable(int row, int col) {
				if(col == 0) {
					return true;
				}else {
					return false;
				}
			}
*/		};
		sorter.setTableHeader(table.getTableHeader());

		table.getTableHeader().setReorderingAllowed(false);
		table.setDragEnabled(true);
		
		JScrollPane scroller = new JScrollPane();
		JViewport vp = scroller.getViewport();
		//scroller.setViewportView(table);

		JToolBar toolbarTemplates = new JToolBar();
		toolbarTemplates.setFloatable(false);

		buttonDelTemplates = toolbarTemplates.add(new DelAction(this));
		buttonDelTemplates.setText("Delete");
//		buttonDelTemplates.setToolTipText(getString("Templates.del_accessible_description"));
//		buttonDelTemplates.setIcon(createImageIcon("Templates.del_image", "Templates.del_accessible_description"));
		
		buttonDelAllTemplates = toolbarTemplates.add(new DelAllAction(this));
		buttonDelAllTemplates.setText("Delete All");
//		buttonDelAllTemplates.setToolTipText(getString("Templates.delAll_accessible_description"));
//		buttonDelAllTemplates.setIcon(createImageIcon("Templates.delAll_image", "Templates.delAll_accessible_description"));

		vp.add(table);
		
		getViewerPanel().add(toolbarTemplates, BorderLayout.NORTH);
		getViewerPanel().add(scroller, BorderLayout.CENTER);

		return table;
	}

	private JTable getTemplatesTable() {
		return templatesTable;
	}

	private void setTemplatesTable(JTable table) {
		templatesTable = table;
	}

//	private Vector<Vector> getTemplatesData() {
	private Vector getTemplatesData() {
//		Vector<Vector> vRow = new Vector<Vector>();
		Vector vRow = new Vector();
		String	strQuery = "SELECT * FROM Templates ORDER BY templateID ASC";
			
		DBConnector.connect();
		ResultSet rsAll = DBConnector.select(strQuery);
		
		try{
			while(rsAll.next()) {
//				Vector<Object> vCol = new Vector<Object>();
				Vector vCol = new Vector();
				vCol.add(new Boolean(false));
				vCol.add(new Integer(rsAll.getInt("templateID")));
				vCol.add(rsAll.getString("templateEnglish"));
				vCol.add(rsAll.getString("templateFilipino"));
				vCol.add(rsAll.getString("compIDEnglish"));
				vCol.add(rsAll.getString("compIDFilipino"));
				vRow.add(vCol);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}

		DBConnector.close();

		if(vRow.size() == 0) {
			vRow = null;
		}
	
		return vRow;
	}

	private Vector getSelTemplates() {
		Vector vSelectedID = new Vector();

		for(int i = 0; i < getTemplatesTable().getRowCount(); i++) {
			//checkbox
			if(((Boolean)getTemplatesTable().getValueAt(i, 0)) == Boolean.TRUE) {
				//id
				vSelectedID.add(getTemplatesTable().getValueAt(i, 1));
			}
		}

		if(vSelectedID.size() == 0) {
			vSelectedID = null;
		}
	
		return vSelectedID;
	}

	private void delSelTemplates(Vector vIDs) {
		for(int i = 0; i < vIDs.size(); i++) {
			delTemplate(vIDs.get(i));
		}
	}

	private void delTemplate(Object oID) {
		String	strQuery = "DELETE FROM Templates WHERE templateID = "+oID;
			
		DBConnector.connect();
		boolean bCheck = DBConnector.update(strQuery);
		
		if(!bCheck)
			System.out.println("There was a problem while deleting Template: "+oID+".");
		else
			System.out.println("Successfully deleted Template: "+oID+".");

		DBConnector.close();
	}
	
	private void delAllTemplates() {
		String	strQuery = "DELETE FROM Templates";
			
		DBConnector.connect();
		boolean bCheck = DBConnector.update(strQuery);
		
		if(!bCheck)
			System.out.println("There was a problem while deleting the Templates.");
		else
			System.out.println("Successfully deleted ALL Templates.");

		DBConnector.close();
	}
	
	class DelAction extends AbstractAction {
		TemplatesViewer templatesViewer;
		private final String LINE_BREAK = "\n";
		private final String COMMA_SPACE = ", ";
	
		protected DelAction(TemplatesViewer templatesViewer) {
			super("DeleteAllTemplatesAction");
			this.templatesViewer = templatesViewer;
		}

		public void actionPerformed(ActionEvent e) {
			Vector vSelID = templatesViewer.getSelTemplates();
			if(vSelID == null) {
				JOptionPane.showMessageDialog(null, "There is no template selected. Please select one or more\ntemplates by clicking the checkbox on the delete column.", null, JOptionPane.WARNING_MESSAGE);
			}else{
				//confirmation
				StringBuffer strMessage = new StringBuffer("Are you sure you want to delete the checked Templates?");
				
				int selectedIndex = JOptionPane.showConfirmDialog(null, strMessage, null, JOptionPane.YES_NO_OPTION);
				//YES
				if(selectedIndex == 0) {
					templatesViewer.delSelTemplates(vSelID);
					//refresh
					if(templatesViewer.getGUI() != null) {
						templatesViewer.getGUI().refreshTable(templatesViewer);
					}
				}
			}
		}
	}

	class DelAllAction extends AbstractAction {
		TemplatesViewer templatesViewer;
	
		protected DelAllAction(TemplatesViewer templatesViewer) {
			super("DeleteTemplatesAction");
			this.templatesViewer = templatesViewer;
		}

		public void actionPerformed(ActionEvent e) {
			//confirmation
			int selectedIndex = JOptionPane.showConfirmDialog(null, "Are you sure that you want to delete ALL templates?", null, JOptionPane.YES_NO_OPTION);
			//YES
			if(selectedIndex == 0) {
				templatesViewer.delAllTemplates();
				//refresh
				if(templatesViewer.getGUI() != null) {
					templatesViewer.getGUI().refreshTable(templatesViewer);
				}
			}
		}
	}
}